//
//  EmotionViewModel.swift
//  AnimotionKeyboard
//
//  Created by xxy on 16/6/3.
//  Copyright © 2016年 xxy. All rights reserved.
//

import Foundation
/// 表情包管理器
class EmotionMangeger {
    /// 单例
    static let sharedManager  = EmotionMangeger()
    
    /// 表情包数组
    lazy var packages  = [EmotionPackage]()
    
    private init() {
        // 0. 添加最近分组
        packages.append(EmotionPackage(dict: ["group_name_cn": "最近"]))
        //1:emotions路径
        guard  let path  = NSBundle.mainBundle().pathForResource("emoticons", ofType: "plist", inDirectory: "Emoticons.bundle") else {
            print("emotions文件不存在")
            return
        }
        //2:记载字典(总字典路径)
        guard let dict  = NSDictionary(contentsOfFile: path) else {
            print("数据格式错我")
            return
        }
        //3:提取数据
        let  array = (dict["packages"] as! NSArray).valueForKey("id") as! [String]
        //4:遍历数组取各个分组数据,转模型
        for id in array {
            loadInfoPlist(id)
        }
    }
    
    
    /// 加载 id 目录下的 info.plist 文件
    private func loadInfoPlist(id: String) {
        //1:创建路径
        let  path = NSBundle.mainBundle().pathForResource("info", ofType: "plist", inDirectory: "Emoticons.bundle/\(id)")!
        //2:加载字典
        let  dict = NSDictionary(contentsOfFile: path) as! [String: AnyObject]
        //3:转模型
        packages.append(EmotionPackage(dict: dict))
        
        
    }

}